// pages/shop-detail/index.js
var app = getApp();
var ddapi = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id:'',
    shop:[],
    ping_num:0,
    product_xian:[],
    product_all:[],
    pinglun:[],
    xing:[],
    hidden: true,
    fen:5,
    page:1,
    page1:1,
    page_count:5,
    shop_activity_len:0,
    product_text:'查看更多',
    pinglun_text: '查看更多',
    qiang:false,
    qiang_text:6,
    display:'none',
    open: 'chai', 
    open_state: true,
    qiang_money:0,
    article:'',
    xuan_1: 'active',
    xuan_2: '',
    xuan_3: '',
    viewHidden_1: false,
    viewHidden_2: true,
    viewHidden_3: true,
    shop_red:'none',
    openid: '',
    user_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
    var intervalId = setInterval(function () {
      var c = that.data.qiang_text
     
      if(c>1){
        c = c - 1;
        that.setData({
          qiang_text: c,
          qiang:false
        })
      }else{
        that.setData({
          qiang_text: '抢',
          qiang: true
        })
      }
    }, 1000)
    var shop_id = e.id;

    that.setData({
      shop_id: shop_id,
      openid: wx.getStorageSync('openid'),
      user_id: wx.getStorageSync('user_id'),
    });

    that.getShop();
    that.getPlunList();
  },
  getShop:function(){
    var that = this;
    var shop_id = that.data.shop_id;
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var user_jingwei = wx.getStorageSync('user_jingwei');
    var service = 'Shop.GetNewShopInfo';
    var sign = ddapi.md5(openid + service + shop_id + user_id + user_jingwei);
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.GetNewShopInfo',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        shop_id: shop_id,
        openid: openid,
        user_id: user_id,
        user_jingwei: user_jingwei
      },
      success: function (res) {
        wx.hideToast()
        console.log(res)
        var shop = res.data.data.info
        if (shop) {
          that.saveLookShop(shop);
        }

        if (shop.shop_product.length > 0) {
          var product_xian = []
          var product_all = []
          for (var i = 0; i < shop.shop_product.length; i++) {
            if (i < 5) {
              product_xian.push(shop.shop_product[i])
            }
            product_all.push(shop.shop_product[i])
          }
        }
        var xing = [];
        if (shop.shop_fen > 0 && shop.shop_fen < 1) {
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
          for (var x = 0; x < 4; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
          }
        } else if (shop.shop_fen == 1) {
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          for (var x = 0; x < 4; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
          }
        } else if (shop.shop_fen > 1 && shop.shop_fen < 2) {
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
          for (var x = 0; x < 3; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
          }
        } else if (shop.shop_fen == 2) {
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          for (var x = 0; x < 3; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
          }
        } else if (shop.shop_fen > 2 && shop.shop_fen < 3) {
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
          for (var x = 0; x < 2; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
          }
        } else if (shop.shop_fen == 3) {
          for (var x = 0; x < 3; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          }
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
        } else if (shop.shop_fen > 3 && shop.shop_fen < 4) {
          for (var x = 0; x < 3; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          }
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
        } else if (shop.shop_fen == 4) {
          for (var x = 0; x < 4; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          }
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
        } else if (shop.shop_fen > 4 && shop.shop_fen < 5) {
          for (var x = 0; x < 4; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          }
          xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
        } else {
          for (var x = 0; x < 5; x++) {
            xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
          }
        }
        if (shop.shop_fen > 0) {
          var fen = shop.shop_fen
        } else {
          var fen = 5
        }
        var temp = WxParse.wxParse('article', 'html', shop.shop_content, that, 5);
        if (shop.shop_red_id!=0){
          that.setData({
            shop_red:'block'
          })
        }

        that.setData({
          shop: shop,
          product_xian: product_xian,
          product_all: product_all,
          xing: xing,
          fen: fen,
          shop_activity_len: shop.shop_activity.length,
          article: temp,
          hidden: false
        });
      },
       fail : function (res) {
         wx.showToast({
           title: '网络错误',
         })
       }
    })
  },
  getPlunList:function(){
    var that = this
    var shop_id = that.data.shop_id
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var service = 'Shop.GetShopPinglun';
    var page = that.data.page
    var page_count = that.data.page_count
    var sign = ddapi.md5(openid + page + page_count + service + shop_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.GetShopPinglun',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        shop_id: shop_id,
        openid: openid,
        user_id: user_id,
        page:page,
        page_count:page_count
      },

      success: function (res) {
        console.log(res)
        var pinglun_all = that.data.pinglun
        var pinglun = res.data.data.info
        if (pinglun.length>0){
          for (var i = 0; i < pinglun.length;i++){
            var xing = [];
            if (pinglun[i].xingji > 0 && pinglun[i].xingji < 1) {
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
              for (var x = 0; x < 4; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
              }
            } else if (pinglun[i].xingji == 1) {
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              for (var x = 0; x < 4; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
              }
            } else if (pinglun[i].xingji > 1 && pinglun[i].xingji < 2) {
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
              for (var x = 0; x < 3; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
              }
            } else if (pinglun[i].xingji == 2) {
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              for (var x = 0; x < 3; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
              }
            } else if (pinglun[i].xingji > 2 && pinglun[i].xingji < 3) {
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
              for (var x = 0; x < 2; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
              }
            } else if (pinglun[i].xingji == 3) {
              for (var x = 0; x < 3; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              }
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
            } else if (pinglun[i].xingji > 3 && pinglun[i].xingji < 4) {
              for (var x = 0; x < 3; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              }
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
            } else if (pinglun[i].xingji == 4) {
              for (var x = 0; x < 4; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              }
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0star.png');
            } else if (pinglun[i].xingji > 4 && pinglun[i].xingji < 5) {
              for (var x = 0; x < 4; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              }
              xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/0.5star.png');
            } else {
              for (var x = 0; x < 5; x++) {
                xing.push('https://hongbao.zntech-china.com/attachment/images/wxcx/1star.png');
              }
            }
            pinglun[i].xingji = xing
            //评论数据转数组
            if (pinglun[i].pinglun_picture.length >0){
              var pinglun_img = pinglun[i].pinglun_picture;
              var pinglun_img_array = [];
              for (var a = 0; a < pinglun_img.length; a++){
                pinglun_img_array.push(pinglun_img[a].imgurl);
              }
              pinglun[i].pinglun_picture = pinglun_img_array;
            }
          }
        }
        console.log(pinglun);
        console.log('评论数据');
        if (pinglun.length>0){
          var pinglun_text = '查看更多'
          for (var i = 0; i < pinglun.length; i++) {
            pinglun_all.push(pinglun[i]);
          }
        }else{
          var pinglun_text = '没有了...'
        }
        that.setData({
          pinglun: pinglun_all,
          ping_num: pinglun_all.length,
          pinglun_text: pinglun_text
        });
      }
    })
  },
  
  open_red: function (e) {
    var that = this;
    that.setData({ open: "rotate" })
    setTimeout(function () {
      that.setData({ open: "chai", open_state: false })
    }, 1200)
  },
  click_close_red: function (e) {
    var that = this;
    that.setData({ open: "chai", open_state: true, display: 'none' })
    that.getShop();
  },
  productMore:function(){
    var that = this
    var p_all = that.data.product_all
    var page1 = that.data.page1
    var product_xian = that.data.product_xian

    if (product_xian.length < p_all.length) {
      for (var i = page1 * 5; i < (page1 + 1) * 5; i++) {
        if (p_all.length > i) {
          product_xian.push(p_all[i]);
        }
      }
      that.setData({
        product_xian: product_xian,
        product_text: '查看更多',
        page1: page1 + 1
      })

    } else {
     that.setData({
      product_xian: p_all,
      product_text: '没有了...'
    })
    }
    
    
  },
  pinglunMore:function(){
    var that = this
    var page = Number(that.data.page)+Number(1)
    that.setData({
      page:page
    })
    that.getPlunList()
  },
  callPhone:function(){
    var that = this
    var phone = that.data.shop.shop_tel
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("成功拨打电话")
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    var share = {
      title: that.data.shop.shop_name + '邀你加入生活圈！',
      path: '',
      imageUrl: that.data.shop.shop_pic_small,
      success: function (res) {
　　　　　　// 转发成功之后的回调
　　　　　　if (res.errMsg == 'shareAppMessage:ok') {
              var openid = wx.getStorageSync('openid');
              var user_id = wx.getStorageSync('user_id'); 
              var data_id = that.data.shop_id
              var zhuanfa_type = 'shop'
              var service = 'System.ZhuanFa';
              var sign = ddapi.md5(data_id + openid + service + user_id + zhuanfa_type);
              wx.request({
                url: app.globalData.apiUrl + '?service=System.ZhuanFa',
                header: {
                  'Content-Type': 'json'
                },
                data: {
                  sign: sign,
                  open_id: openid,
                  user_id: user_id,
                  data_id: data_id,
                  zhuanfa_type: zhuanfa_type
                },

                success: function (res) {
                  console.log(res)
                  wx.showToast({
                    title: '转发成功'
                  })
                }
              })
　　　　　　}
　　　　}
    };
    
    return share
  },
  openLocation: function () {
    var that = this
    var latlng = that.data.shop.shop_address_jingwei
    console.log(latlng)
    var slatlng = latlng.split(',')
      wx.openLocation({
        latitude: Number(slatlng[0]),
        longitude: Number(slatlng[1]),
        scale: 16, // 缩放比例
      })
  },
  saveLookShop: function (shop) {
    console.log('aaaa');
    var that = this
    var newDic = {};
    var lookShopList = [];
    var newLookShopList = [];

    if (wx.getStorageSync('lookShopList')) {
      lookShopList = wx.getStorageSync('lookShopList');
    }

    newDic.shop_id = that.data.shop_id;
    newDic.shop_name = shop.shop_name;
    newDic.shop_pic_small = shop.shop_pic_small;

    if (lookShopList.length >= 30) {
      console.log('bbbb');
      for (var i = lookShopList.length - 20; i < lookShopList.length; i++) {
        if (dic.shop_id != newDic.shop_id) {
          newLookShopList.push(lookShopList[i]);
        }
      }

      newLookShopList.push(newDic);
      wx.setStorageSync('lookShopList', newLookShopList);
      
   
      console.log(newLookShopList);
    } else {
      console.log('cccc');
      newLookShopList = [];

      var bool = false;
      for (var i = 0; i < lookShopList.length; i++) {
        var dic = lookShopList[i];
        if (dic.shop_id != newDic.shop_id) {
          newLookShopList.push(lookShopList[i]);
        }
      }

      newLookShopList.push(newDic);
      wx.setStorageSync('lookShopList', newLookShopList);
      console.log(newLookShopList);
    }
  },
  
  changeview:function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    if (id == '1') {
      var xuan_1 = 'active'
      var xuan_2 = ''
      var xuan_3 = ''
      var viewHidden_1 = false
      var viewHidden_2 = true
      var viewHidden_3 = true
    } else if (id == '2') {
      var xuan_1 = ''
      var xuan_2 = 'active'
      var xuan_3 = ''
      var viewHidden_1 = true
      var viewHidden_2 = false
      var viewHidden_3 = true
    } else if (id == '3') {
      var xuan_1 = ''
      var xuan_2 = ''
      var xuan_3 = 'active'
      var viewHidden_1 = true
      var viewHidden_2 = true
      var viewHidden_3 = false
    } else {
      var xuan_1 = 'active'
      var xuan_2 = ''
      var xuan_3 = ''
      var viewHidden_1 = false
      var viewHidden_2 = true
      var viewHidden_3 = true
    }
    that.setData({
      xuan_1: xuan_1,
      xuan_2: xuan_2,
      xuan_3: xuan_3,
      viewHidden_1: viewHidden_1,
      viewHidden_2: viewHidden_2,
      viewHidden_3: viewHidden_3
    })
  },

  imgYu: function (e) {//图片预览
  console.log(e);
    var src = e.currentTarget.dataset.src;//获取data-src
    var imgList = e.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },

})